var start = 1;
var pageSize = 10;
var obj={
    applyPerson:'',
    ebbId:'',
    ebbName:''
}
$(function () {
    loadFarm();
});
function loadFarm() {
    $.ajax({
        type: "get",
        url: "/ebb/admin/ebb/noRegionList/?start=" + start + "&size=" + pageSize + "&type=1&addressCode=520600000000",
        contentType: "application/json",
        dataType: "json",
        success: function (result) {
            if (typeof result != 'object') {
                result = eval('(' + result + ')');
            }
            if (result.data.length < 1) {
                $('#amount').text("0.00");
            }
            if (result.data.length > 0) {
                var totalCount = result.totalPages;
                totalPage = totalCount;
                $("#total-page").text(totalPage);

                $("#pagination-bar").show();
                $("#pagination-box").html('<ul id="pagination" class="pagination"></ul>');
                $('#pagination').twbsPagination({
                    totalPages: totalCount,
                    visiblePages: 5,
                    first: '第一页',
                    prev: '上一页',
                    next: '下一页',
                    last: '最后页',
                    onPageClick: function (event, page) {
                        start = page;
//                        params.index = emp;
                        $.ajax({
                            type: "get",
                            url: "/ebb/admin/ebb/noRegionList/?start=" + start + "&size=" + pageSize + "&type=1&addressCode=520600000000",
                            contentType: "application/json",
                            dataType: "json",
                            success: function (result) {
                                if (typeof result != 'object') {
                                    result = eval('(' + result + ')');
                                }
                                totalSum = result.totalPages;
                                var d = result.data;
                                var totalTradingVolumeByPage = 0;
                                for (var i = 0; i < d.length; i++) {
                                    totalTradingVolumeByPage += parseFloat(d[i].tradingVolume);
                                    if(d[i].platformCategoryParent==1){
                                        d[i].platformCategoryParent="网络零售平台";
                                    }else{
                                        d[i].platformCategoryParent="本地生活类服务平台";
                                    }

                                }
                                totalTradingVolumeByPage = totalTradingVolumeByPage.toFixed(2);
                                $('#amount').text(totalTradingVolumeByPage);
                                selectObj = d;
                                $("input[name='checkboxAll']").prop("checked", false);
                                selectAllId = [];
                                //渲染数据
                                $.templates("#farm-list-tmpl").link("#farm-list-data", d);
                                totalPages = result.totalPages;
                                $('#farm-count').text(result.totalCount);
                                $('#page-total').text(result.totalPages);
                                $('#page-index').val(page);
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                if (XMLHttpRequest.status == 401) {
                                    //跳转到登录页,带上登录后的跳转地址redirect_uri=???
                                    // redirect_login();

                                    ShowTip("你还没有登录，请先登录！", "success", 1000);
                                    setTimeout(function () {
                                        window.location = "login.html";
                                    }, 1000);
                                }


                            }
                        });
                    },

                    startPage: start
                });
            } else {
                $("#farm-list-data").html("<tr><td colspan='8'><h1 align='center' height='300px'>暂无数据</h1></td></tr>");
                $("#pagination-bar").hide();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status == 401) {
                //跳转到登录页,带上登录后的跳转地址redirect_uri=???
                // redirect_login();

                ShowTip("你还没有登录，请先登录！", "success", 1000);
                setTimeout(function () {
                    window.location = "login.html";
                }, 1000);
            }


        }
    });
}


var lock = false;
//$("form[name=apply-form]").submit(function () {});

function submitData() {
    if (lock) {
        alert("不能重复提交");
        window.location = "noRegionList.html";
    }
    lock = true;
    obj.applyPerson= $("#apply_text_id").val();
    $.ajax({
        url: '/ebb/admin/apply/',
        data:JSON.stringify(obj),
        contentType:"application/json",
        type: 'post',
        success: function (data) {
            lock=false;
            console.log(data);
            if (!data.error) {
                ShowTip("认领已成功提交，请等待审核！", "success", 1000);

                setTimeout(function () {

                }, 1000);

            } else {
                lock=false;
                if (data.errorMessage) {
                    ShowTip(data.errorMessage, "error", 1000);

                    setTimeout(function () {

                    }, 1000);
                }
            }
        },
        error: function (error) {
            lock=false;
            var err = JSON.parse(error.responseText);
            if(err.errorDescription=="existed"){
                ShowTip("该条记录已经被你认领，请等待审核！", "error", 1000);
                setTimeout(function () {

                }, 1000);
            }
        }

    });
}





function getData(id,storeName) {
    obj.ebbId=id;
    obj.ebbName=storeName;
}




//获取当前系统年份月份
function getCurrentDate() {
    var date = new Date();
    var year = date.getFullYear();//年
    var mouth = date.getMonth() + 1;
    var day = date.getDate();
    //判断如果是1-9日期则加上0
    if (mouth >= 1 && mouth <= 9) {
        mouth = "0" + mouth;
    }
    var startTime = year + "-" + mouth;

    return startTime;
}

$('#go-index').click(function () {
    var index = $('#page-index').val();
    if (index > totalSum) {
        index = totalSum;
    }
    start = parseInt(index);
    loadFarm();
});
















